<?php

namespace App\Services;

use App\Models\Avatar;
use App\Models\Category;
use App\Models\Comment;
use App\Models\LawOrder;
use App\Models\Question;
use App\Models\User;
use Exception;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;

class QuestionService
{
    /**
     * 创建
     */
    public static function create($param)
    {
        Log::channel('sql')->debug($param);

        // user_source_id 来源用户id
        // user_name 用户名称
        // question_title 提问标题
        // question_content 提问内容
        // question_source 来源
        // comment_content 评论内容
        // law_order_category_one_name 一级类别
        // law_order_category_two_name 二级类别
        // law_order_money 涉案金额
        // law_order_address 地址
        // law_order_issue 大致情况
        // law_order_appeal 诉求
        // law_order_source_id 来源id

        return DB::transaction(function () use ($param) {
            try {
                if ($param['question_source'] == 'law_order') {
                    $lawOrderInfo = LawOrder::query()->where('source_id', $param['law_order_source_id'])->first();
                    if ($lawOrderInfo) {
                        return '已存在来源id';
                    }
                }

                // 查询用户是否存在
                $userInfo = User::query()->where('source_id', $param['user_source_id'])->first();
                if (!$userInfo) {
                    // 随机查询一个头像
                    $avatar = Avatar::query()->inRandomOrder()->value('avatar');

                    $userInfo = [
                        'name' => $param['user_name'],
                        'avatar' => $avatar,
                        // 'password' => Hash::make('123456'),
                        'source_id' => $param['user_source_id'],
                        'created_at' => date("Y-m-d H:i:s"),
                        'updated_at' => date("Y-m-d H:i:s"),
                    ];
                    $userInfo['id'] = User::query()->insertGetId($userInfo);
                }

                // 创建提问
                $questionId = Question::query()->insertGetId([
                    'user_id' => $userInfo['id'],
                    'title' => $param['question_title'],
                    'content' => $param['question_content'],
                    'status' => 1,
                    'comment_status' => 1,
                    'comment_count' => 1,
                    'source' => $param['question_source'],
                    'created_at' => date("Y-m-d H:i:s"),
                    'updated_at' => date("Y-m-d H:i:s"),
                ]);

                // 创建订单数据
                if ($param['question_source'] == 'law_order') {
                    $categoryOneId = Category::query()
                        ->where('title', $param['law_order_category_one_name'])
                        ->where('parent_id', 0)
                        ->value('id');
                    $categoryTwoId = Category::query()
                        ->where('title', $param['law_order_category_two_name'])
                        ->where('parent_id', $categoryOneId)
                        ->value('id');

                    LawOrder::query()->insert([
                        'question_id' => $questionId,
                        'category_one_id' => $categoryOneId,
                        'category_two_id' => $categoryTwoId,
                        'money' => $param['law_order_money'],
                        'address' => $param['law_order_address'],
                        'issue' => $param['law_order_issue'],
                        'appeal' => $param['law_order_appeal'],
                        'source_id' => $param['law_order_source_id'],
                        'created_at' => date("Y-m-d H:i:s"),
                        'updated_at' => date("Y-m-d H:i:s"),
                    ]);

                    Category::query()->where('id', $categoryOneId)->update([
                        'num' => Db::raw('num + 1')
                    ]);
                }

                // 查询评论用户id
                $commentUserId = User::query()->where('name', '重庆朋昇律师事务所')->value('id');
                if (!$commentUserId) {
                    $commentUserId = User::query()->insertGetId([
                        'name' => '重庆朋昇律师事务所',
                        'avatar' => 'https://www.fal.cc/wp-content/uploads/member/avatars/238a0b923820dcc5.1728716659.jpg',
                        'created_at' => date("Y-m-d H:i:s"),
                        'updated_at' => date("Y-m-d H:i:s"),
                    ]);
                }

                // 创建评论
                Comment::query()->insert([
                    'question_id' => $questionId,
                    'user_id' => $commentUserId,
                    'content' => $param['comment_content'],
                    'created_at' => date("Y-m-d H:i:s"),
                    'updated_at' => date("Y-m-d H:i:s"),
                ]);
            } catch (Exception $exception) {
                throw new Exception($exception->getMessage());
            }

            return null;
        }, 5);
    }
}
